var cp = dataProvider;
cp.chartConnection = {
    connectionStatus: true,
    sendMsg: function (msg) {
        console.log("send msg", msg);
    }
};



var newConfig  = {
    command: "config",
    requestId: "abc",
    payload: {
        chartName: 'TestChart',
        chartTitle: "",
        chartType: "line", // line, OHLC
        configSource: "Excel", // Excel ,PTW, Mobile
        parameters: // things that releated to client(like eletrcon), server can ignore this
        {
            frameless: true,
            xpos: 200,
            ypos: 300,
            height: 300,
            width: 400,
        },
        chartOptions: {
            //things related to highchart, like font size, line style , this can be persisted
        },
        dataOptions: {
            startTime: 1552579204000,
            endTime: 1552579204000, // if null, it contains subscribe
            sampleInterval: -60 * 1000, //if <= 0, means it's tickdata
            maxDataPoint: 60, //this is only for tickData
            businessHour: "9:00-11:00, 13:00-15:30",
        },
        rangeConfigs: [{
            columnNames: ["adjBidPx", "adjMidPx", "adjAskPx"],
            rangeName: "AdjPriceRT2Graph",
            rowNames: ["170303.IB"],
        }],
        queryConfigs: [{
            query: "select filed1, filed2 from whateverData where filter = 'xxxx'",
            xValueField: "messagePublishTime"
        }],
        directionConfigs: [{
            tokens: ["f5.c1.p", "f10.c2", "2*s3m-100100.IB"],
            quarter: "z8,h9,m9",
            ctd: "170007,170007,170007,170004,170004,170004",
            mergestrategy: "Union" //If not use anymore, we can remove this
        }],
    }
};


function t_sendNewChartConfig() {
    cp.onMessage(newConfig);
}

function t_sendNewStart(requestId) {
    var startMark = {
        requestId: requestId,
        msgType: "startMark",
        payload: {
            chartName: "",
            sampleInterval: 60 * 1000, //if <= 0, means it's tickdata
            maxDataPoint: 5000 // optional field
        }
    };
    cp.onMessage(startMark);
}

function t_sendNewEnd(requestId) {
    var endMark = {
        requestId: requestId,
        msgType: "endMark",
        payload: {
            chartName: "",
            sampleInterval: 60 * 1000, //if <= 0, means it's tickdata
            maxDataPoint: 5000 // optional field
        }
    };
    cp.onMessage(endMark);
}

function t_sendError(requestId) {
    var errorMsg = {
        requestId: requestId,
        msgType: "Error",
        payload: {
            seriesName: "f5.c1.p",
            errorCode: 1,
            errorMsg: "Missing data for f5.c1.p"
        }
    };
    cp.onMessage(errorMsg);
}

function t_sendNewTick(seriesName,requestId) {
    // console.log("t_sendNewTick",seriesName,requestId);
    var tickdata = {
        requestId: requestId,
        msgType: "CHART_DATA",
        payload: {
            seriesName: "f5.c1.p",
            dataType: "Ticked",
            data: []
        }
    };
    if (isValued(seriesName)) {
        tickdata.payload.seriesName = seriesName;
    }
    var d = new Date();
    d.setMilliseconds(0);
    var t = d.getTime();
    var data = {
        x: t,
        y: parseFloat((3 + Math.round(Math.random() * 100) / 100).toFixed(2))
    }
    tickdata.payload.data.push(data);
    cp.onMessage(tickdata);
}

function t_sendNewQueryTick(requestId) {
    var tickdata1 = {
        requestId: requestId,
        msgType: "CHART_DATA",
        payload: {
            seriesName: "f5.c1.p",
            dataType: "Ticked",
            data: []
        }
    };
    var tickdata2 = {
        requestId: requestId,
        msgType: "CHART_DATA",
        payload: {
            seriesName: "f10.c2.p",
            dataType: "Ticked",
            data: []
        }
    };
    var d = new Date();
    d.setMilliseconds(0);
    var t = d.getTime();
    // t.setMilliseconds(0);
    for (var i = -50000; i < 0; i += 1000) {
        var data = {
            x: t + i,
            y: parseFloat((3 + Math.round(Math.random() * 100) / 100).toFixed(2))
        }
        var data2 = {
            x: t + i,
            y: parseFloat((3 + Math.round(Math.random() * 100) / 100).toFixed(2))
        }
        tickdata1.payload.data.push(data);
        tickdata2.payload.data.push(data2);
    }
    cp.onMessage(tickdata1);
    cp.onMessage(tickdata2);
}

function t_sendSampleHistOHLCData(seriesName,requestId) {

    var sampleData = {
        requestId: requestId,
        msgType: "CHART_DATA",
        payload: {
            seriesName: seriesName,
            dataType: "Sampled",
            data: []
        }
    };

    var d = new Date();
    d.setSeconds(0);
    d.setMilliseconds(0);
    t = d.getTime();
    for (var i = -50000 * 60; i < 0; i += 1000 * 60) {
        var temp = parseFloat((3 + Math.round(Math.random() * 100) / 100).toFixed(2));
        var d = {
            x: t + i,
            open: ohlcData[-i / 6000][1],
            high: ohlcData[-i / 6000][2],
            low: ohlcData[-i / 6000][3],
            close: ohlcData[-i / 6000][4],
            // y:ohlcData[-i/6000][4]
        }
        sampleData.payload.data.push(d);
    }
    cp.onMessage(sampleData);
}

function t_sendMoreSampleHistOHLCData(seriesName,requestId){
    var sampleData = {
        requestId: requestId,
        msgType: "CHART_DATA",
        payload: {
            seriesName: seriesName,
            dataType: "Sampled",
            data: []
        }
    };

    var d = new Date();
    d.setSeconds(0);
    d.setMilliseconds(0);
    t = d.getTime();
    for (var i = -50000 * 60; i < 0; i += 1000 * 60) {
        var temp = parseFloat((3 + Math.round(Math.random() * 100) / 100).toFixed(2));
        var d = {
            x: t + i*60,
            open: ohlcData[-i / 6000][1],
            high: ohlcData[-i / 6000][2],
            low: ohlcData[-i / 6000][3],
            close: ohlcData[-i / 6000][4],
            // y:ohlcData[-i/6000][4]
        }
        sampleData.payload.data.push(d);
    }
    cp.onMessage(sampleData);
}

function t_sendOHLCSubData(seriesName,requestId) {
    // console.log("t_sendOHLCSubData",seriesName,requestId);
    var sampleData = {
        requestId: requestId,
        msgType: "CHART_DATA",
        payload: {
            seriesName: seriesName,
            dataType: "Ticked",
            data: []
        }
    };
    var t = new Date().getTime();
    var d = {
        x: t,
        y: ohlcData[Math.floor(Math.random() * 100)][4]
    }
    sampleData.payload.data.push(d);
    // console.log("t_sendSubData",d);
    cp.onMessage(sampleData);
}